package jdbcForMe;


public class FormattedForm {
    private Object[][] form;
    private Object[] rowName;
    private int tail = -1;
    private int size = 0;

    public FormattedForm() {
        form = new Object[5][5];
        rowName = new Object[5];
        size = 5;
    }

    public FormattedForm(int size) {
        form = new Object[size][size];
        rowName = new Object[size];
        this.size = size;
    }

    public FormattedForm(String... row) {
        size = row.length;
        form = new Object[size][size];
        rowName = row;
        System.arraycopy(row, 0, form[0], 0, rowName.length);
//        for (int i = 0; i < row.length; i++) {
//            form[0][i] = row[i];
//        }
    }

    /**
     * 添加标题
     *
     * @param titles 标题数据
     */
    public void fillTheTop(String... titles) {
        //判断contents长度是否合法
        //判断数据容量时候足够
        //判断top是否为空
        rowName = titles;
        System.arraycopy(titles, 0, form[0], 0, rowName.length);
    }

    /**
     * 添加一行数据
     *
     * @param contents 所需要添加的数据
     */
    public void fillContents(String... contents) {
        //判断contents长度是否合法
        //判断数据容量时候足够
        //判断contents是否为空（-1为空）
        for (int i = 0; i < rowName.length; i++) {
            form[tail][i] = contents[i];
        }
        tail++;
    }

    @Override
    public String toString() {
        /**
         * row1 row2 row3 row4
         * v1   v2   v3   v4
         * v1   v2   v3   v4
         * v1   v2   v3   v4
         * v1   v2   v3   v4
         * v1   v2   v3   v4
         * v1   v2   v3   v4
         */

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < form.length; i++) {
            for (int i1 = 0; i1 < rowName.length; i1++) {
                sb.append(form[i][i1]);
                if (i1 == rowName.length - 1) {
                    sb.append("\n");
                } else {
                    sb.append("\t");
                }
            }
        }
        return sb.toString();
    }
}
